<?php
include("interface.class.php");
include("table.class.php");
include("database.class.php");

$inf = new webinterface();
$inf->set_title("Add New Proxys");

if (array_key_exists('step', $_POST) && is_numeric($_POST['step'])) {

	switch ($_POST['step']) {
		case 1:
			if (array_key_exists('raw', $_POST)) {
				$lines = split("\n", $_POST['raw']);

				$tab = new table();
				$tab->t_id="newproxys";
				$tab->set_head(array('#', 'IP', 'Port', 'User', 'Password'));
				$count = 1;
				foreach ($lines as $line){
					$check = "<INPUT TYPE='checkbox' name='proxy" . $count . "' CHECKED />";
					if (eregi("([a-z0-9\-_]+):([a-z0-9\-_]+)@([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+):([0-9]+)", $line, $regs)){
						$user	= $regs[1];
						$passwd	= $regs[2];
						$ip	= $regs[3];
						$port	= $regs[4];
						if (checkValidIp($ip) and $port < 65536) {
							$check .= "<INPUT TYPE='hidden' name='ip" . $count . "' value='" . $ip . "' />";
							$check .= "<INPUT TYPE='hidden' name='port" . $count . "' value='" . $port . "' />";
							$check .= "<INPUT TYPE='hidden' name='user" . $count . "' value='" . $user . "' />";
							$check .= "<INPUT TYPE='hidden' name='passwd" . $count . "' value='" . $passwd . "' />";
							$table_row = array($check, $ip, $port, $user, $passwd);
							$tab->add_row($table_row);
							$count = $count + 1;
						}
					} 
					elseif (eregi("([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+):([0-9]+)", $line, $regs)){
						$ip	= $regs[1];
						$port	= $regs[2];
						if (checkValidIp($ip) and $port < 65536) {
							$check .= "<INPUT TYPE='hidden' name='ip" . $count . "' value='" . $ip . "' />";
							$check .= "<INPUT TYPE='hidden' name='port" . $count . "' value='" . $port . "' />";
							$check .= "<INPUT TYPE='hidden' name='user" . $count . "' value='' />";
							$check .= "<INPUT TYPE='hidden' name='passwd" . $count . "' value='' />";
							$table_row = array($check, $ip, $port, "", "");
							$tab->add_row($table_row);
							$count = $count + 1;
						}
					} 
					elseif (eregi("([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)[^0-9]+([0-9]+)", $line, $regs)){
						$ip	= $regs[1];
						$port	= $regs[2];
						if (checkValidIp($ip) and $port < 65536) {
							$check .= "<INPUT TYPE='hidden' name='ip" . $count . "' value='" . $ip . "' />";
							$check .= "<INPUT TYPE='hidden' name='port" . $count . "' value='" . $port . "' />";
							$check .= "<INPUT TYPE='hidden' name='user" . $count . "' value='' />";
							$check .= "<INPUT TYPE='hidden' name='passwd" . $count . "' value='' />";
							$table_row = array($check, $ip, $port, "", "");
							$tab->add_row($table_row);
							$count = $count + 1;
						}
					} 

				}
				$tab->add_feature("bPaginate", "false");
				$inf->add_docready_script($tab->script());
				$inf->add_content('<form method="POST"><input type="hidden" name="step" value="2" />');
				$inf->add_content($tab->html());
				$inf->add_content('<input type="submit" value="Finish" /></form>');
			}
			break;
		case 2:
			$finish = False;
			$count = 1;
			$db = new database();
			$link = $db->connect();
			$inserted = 0;
			do {
				if (	array_key_exists('proxy' . $count, $_POST) &&
					array_key_exists('ip' . $count, $_POST) &&
					array_key_exists('port' . $count, $_POST) &&
					array_key_exists('user' . $count, $_POST) &&
					array_key_exists('passwd' . $count, $_POST)) 
				{
					$accept	= $_POST['proxy' . $count];
					$ip 	= $_POST['ip' . $count];
					$port 	= $_POST['port' . $count];
					$user 	= $_POST['user' . $count];
					$passwd	= $_POST['passwd' . $count];
					$count++;
					if (!checkValidIP($ip) or $port < 0 or $port > 65535 or $accept != "on") {
						continue;
					}
					if ($user == "" or !eregi("[a-z0-9\-_]+")) {
						$user = "";
					}
					if ($passwd == "" or !eregi("[a-z0-9\-_]+")) {
						$passwd = "";
					}

					if(add_proxy($link, $ip, $port, $user, $passwd)){
						$inserted ++;
					}
				} else {
					$finish = True;
				}
			} while( ! $finish );
			mysql_close($link);
			if ($inserted > 0) {
				$inf->add_content('<h2>' . $inserted . ' proxys was added!</h2>');
			} elseif ($inserted == 1) {
				$inf->add_content('<h2>' . $inserted . ' proxy was added!</h2>');
			} else {
				$inf->add_content('<h2>No proxy was added!</h2>');
			}
			break;
	}
	
} else {
	$inf->add_script('<script type="text/javascript">
		$(function() {
			$("#tabs").tabs();
		});
		</script>
	');
	$inf->add_content('<div id="tabs">
		<ul>
			<li><a href="#tabs-1">CSV List</a></li>
			<li><a href="#tabs-2">RAW Data</a></li>
		</ul>');

	$inf->add_content('<div id="tabs-1"></div>'); //Tabs end
	$inf->add_content('<div id="tabs-2">
	<form method="POST">
	<input type="hidden" name="step" value="1" />
	<textarea name="raw" cols="75" rows="20"></textarea>
	<input type="submit" value="Next" />
	</form>
	</div>'); // RAW
	$inf->add_content('</div>'); //Tabs end
}

$inf->commit();

function checkValidIp($cidr) {
    // Checks for a valid IP address or optionally a cidr notation range
    // e.g. 1.2.3.4 or 1.2.3.0/24
   if(!eregi("^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$", $cidr)) {
       $return = FALSE;
   } else {
       $return = TRUE;
   }
   
    if ( $return == TRUE ) {
        $ip = $cidr;
        $octets = explode(".", $ip);

        foreach ( $octets AS $octet ) {
            if ( $octet > 255 ) {
                $return = FALSE;
            }
        }

    }

    return $return;
}

function add_proxy($dblink, $ip, $port, $user, $passwd) {
	if ($user == "" or $password == ""){
		$sql = "SELECT * FROM proxys WHERE ip=INET_ATON('" . $ip . "') AND port='" . $port . "' AND user IS NULL AND password IS NULL";	
		$sqlinsert = "INSERT INTO proxys (ip, port) VALUES (INET_ATON('" . $ip . "'), " . $port . ")";
	} else {
		$sql = "SELECT * FROM proxys WHERE ip=INET_ATON('" . $ip . "') AND port='" . $port . "' AND user='" . $user . "' AND password='" . $passwd . "'";	
		$sqlinsert = "INSERT INTO proxys (ip, port, user, password) VALUES (INET_ATON('" . $ip . "'), " . $port . ", '" . $user . "', '" . $passwd . "')";
	}
	$res = mysql_query($sql, $dblink);
	if (!$row = mysql_fetch_assoc($res)){
		$res = mysql_query($sqlinsert, $dblink);	
		return True;
	} else {
		return False;
	}
}

?>
